package com.example.coffeeseller_2.views.fragments;


import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.factory.FragmentEnum;
import com.example.coffeeseller_2.utils.MyLog;
import com.example.coffeeseller_2.utils.Waiter;
import com.example.coffeeseller_2.views.HomeActivity;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

/**
 * A simple {@link Fragment} subclass.
 */
public class  WelcomeFragment extends BasicFragment {

    private TextView helloText;
    private View view;
    HomeActivity homeActivity;
    private int count = 60;
    private Handler handler;
    private static final String TAG = "WelcomeFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_welcome, container, false);
        initView();
        return view;
    }

    private void initView() {
        helloText = (TextView) view.findViewById(R.id.helloText);
        homeActivity = HomeActivity.getInstance();
        Typeface typeface = Typeface.createFromAsset(homeActivity.getAssets(), "fonnts/锐字云字库大标宋体.ttf");
        helloText.setTypeface(typeface);
        handler = new Handler();
        intentToCheckPage();

    }

    private void intentToCheckPage() {
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean isStop = false;
                count = 60;

                while (!isStop) {

                    isStop = detectBaidu();
                    if (isStop) {

                        count = 0;
                        checkResult(isStop);

                    } else {
                        if (count == 0) {

                            checkResult(isStop);
                            isStop = true;

                        } else {
                            count--;
                        }
                    }

                    Waiter.doWait(1000);

                }
            }
        });
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                thread.start();
            }
        }, 5000);


    }

    private void checkResult(final boolean isStop) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (!isStop) {
                    Toast.makeText(homeActivity, "网络不可用！", Toast.LENGTH_LONG).show();
                }
                homeActivity.replaceFragment(FragmentEnum.WelcomeFragment, FragmentEnum.MachineCheckFragment);

            }
        });
    }

    public boolean detectBaidu() {

        boolean flag = false;
        int code = -1;
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        try {
            HttpClient client = new DefaultHttpClient();
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);
            HttpResponse httpResponse = client.execute(httpGet);
            code = httpResponse.getStatusLine().getStatusCode();

            if (code == 200 || code == 201) {

                flag = true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //locateError(e);
            //e.printStackTrace();
        }
        MyLog.d(TAG, "code = " + code);
        return flag;
    }

}
